package data.utils;

import java.util.HashMap;
import java.util.Map;

import data.Gene;

public class EuclideanDistance implements GeneDistanceCalculator {
	
	private int count=0;

	private Map<Line,Double> cached;
	
	public EuclideanDistance() {
		cached = new HashMap<Line, Double>();
	}
	
	@Override
	public double distance(Gene g1, Gene g2) {
		Line line = new Line(g1,g2);
		if (cached.containsKey(line)) {
			return cached.get(line).doubleValue();
		}
		
		int max = Math.min(g1.getVeriables().length, g2.getVeriables().length);
		double sum =0;
		for (int i=0; i<max; i++) {
			sum += Math.pow((g1.getVeriables()[i]-g2.getVeriables()[i]),2);
		}
		
		
		double d = Math.sqrt(sum);
		
		cached.put(line, new Double(d));
		
		return d;
	}
}
